<?php
/*********************************************************************
    class.client.php

    Handles everything about client

    The class will undergo major changes one client's accounts are used. 
    At the moment we will play off the email + ticket ID authentication.

    Peter Rotich <peter@osticket.com>
    Copyright (c)  2006-2010 osTicket
    http://www.osticket.com

    Released under the GNU General Public License WITHOUT ANY WARRANTY.
    See LICENSE.TXT for details.

    vim: expandtab sw=4 ts=4 sts=4:
    $Id: $
**********************************************************************/

class Client {


    var $id;
    var $fullname;
    var $username;
    var $passwd;
    var $email;

    
    var $udata;
    var $ticket_id;
    var $ticketID;

  
    function Client($email,$id=0){  //if we miss id parameter on creating an object that will be ok
        $this->id=0;
        if ($id==0) {
        return ($this->lookup2($email)); //returns NULL if user don't have tickets
        } else {
               return ($this->lookup($id,$email)); //returns NULL if user don't have tickets  
        }
    }

    function isClient(){
        return TRUE;
    }

    function lookup($id,$email=''){
        $sql='SELECT ticket_id,ticketID,name,email FROM '.TICKET_TABLE.' WHERE ticketID='.db_input($id);
        if($email){ //don't validate...using whatever is entered.
            $sql.=' AND email='.db_input($email);
        }
        $res=db_query($sql);
        if(!$res || !db_num_rows($res))
            return NULL;

        /* Faking most of the stuff for now till we start using accounts.*/
        $row=db_fetch_array($res);
        $this->udata=$row;
        $this->id         = $row['ticketID']; //placeholder
        $this->ticket_id  = $row['ticket_id'];
        $this->ticketID   = $row['ticketID'];
        $this->fullname   = ucfirst($row['name']);
        $this->username   = $row['email'];
        $this->email      = $row['email'];
      
        return($this->id);
    }

    function lookup2($email){
        $sql='SELECT email_id,email,name,userpass FROM '.EMAIL_TABLE.' WHERE email='.db_input($email);
        $res=db_query($sql);
        if(!$res || !db_num_rows($res))
            return NULL;

        /* Faking most of the stuff for now till we start using accounts.*/
        $row=db_fetch_array($res);
        $this->udata=$row;
        $this->id         = $row['email_id']; //placeholder
        $this->ticket_id  = $row['email_id'];
        $this->ticketID   = $row['email_id'];
        $this->fullname   = ucfirst($row['name']);
        $this->username   = $row['email'];
        $this->email      = $row['email'];
        $this->passwd     = $row['userpass'];
      
        return($this->id);
    }
    
    /*compares user password*/
    function check_passwd($password){
        return (strlen($this->passwd) && strcmp($this->passwd, MD5($password))==0)?(TRUE):(FALSE);
    }

    function getId(){
        return $this->id;
    }

    function getEmail(){
        return($this->email);
    }

    function getUserName(){
        return($this->username);
    }

    function getName(){
        return($this->fullname);
    }
    
    function getTicketID() {
        return $this->ticketID;
    }
    
    function create($vars,&$errors) {
            
        
        if(!$vars['email'] || !Validator::is_email($vars['email']))
            $errors['email']='Valid email required';
        elseif(Email::getIdByEmail($vars['email']))
            $errors['email']='Already in-use system email';


        if($vars['npassword'] || $vars['vpassword']){
            if(!$vars['npassword'])
                $errors['npassword']='Temp password required';
            elseif($vars['npassword'] && strcmp($vars['npassword'],$vars['vpassword']))
                $errors['vpassword']='Password(s) do not match';
            elseif($vars['npassword'] && strlen($vars['npassword'])<6)
                $errors['npassword']='Must be at least 6 characters';
        } 
        //start to create a sql query to create a new user
        if(!$errors){
            $empty ='';
            $sql=' SET name='.db_input($vars['name']).
                 ',email='.db_input($vars['email']).
                 ',userid='.db_input($vars['email']). 
                 ',userpass='.db_input(md5($vars['npassword'])).
                 ',mail_host='.db_input($empty).
                 ',smtp_host='.db_input($empty).
                 ',created = NOW()'.
                 ',updated = NOW()';            

            $sql='INSERT INTO '.EMAIL_TABLE.' '.$sql;
            //echo $sql.'<br/>';
            if(db_query($sql) && ($uID=db_insert_id()))
            return $uID;

            $errors['err']='Unable to create user. Internal error';
 
        }

        return $errors?false:true;
    }
}

?>
